package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Articleinfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @program: demo
 * @author: JCM
 * @create: 2023-08-05 16:03
 * @description:
 **/
@RequestMapping("/art")
@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mylist")
    public AjaxResult getMyList(HttpServletRequest request) {
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if(userinfo == null) {
            return AjaxResult.fail(-1,"非法参数");
        }
        List<Articleinfo> list = articleService.getMyList(userinfo.getId());
        return AjaxResult.success(list);
    }

    @RequestMapping("del")
    public AjaxResult delArt(HttpServletRequest request, Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1,"非法参数");
        }
        // 获取当前用户
        Userinfo userinfo = UserSessionUtils.getUser(request);

        return AjaxResult.success(articleService.delArt(id, userinfo.getId()));
    }

    @RequestMapping("/detail")
    public AjaxResult getDetail(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1,"非法参数！");
        }
        return AjaxResult.success(articleService.getDetail(id));
    }

    @RequestMapping("/updatercount")
    public AjaxResult incRCount(Integer id) {
        if (id != null && id > 0) {
            return AjaxResult.success(articleService.incRCount(id));
        }
        return AjaxResult.fail(-1,"未知错误");
    }

    @RequestMapping("/add")
    public AjaxResult addArt(HttpServletRequest request,Articleinfo articleinfo) {
        // 非空校验
        if (articleinfo == null || articleinfo.getTitle().length() <= 0|| articleinfo.getContent().length() <= 0) {
            return AjaxResult.fail(-1,"非法参数");
        }
        // 添加到数据库
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if (userinfo == null || userinfo.getId() <= 0) {
            return AjaxResult.fail(-2,"无效的登录用户");
        }
        articleinfo.setUid(userinfo.getId());
        return AjaxResult.success(articleService.addArt(articleinfo));
    }

    @RequestMapping("/update")
    public AjaxResult update(Articleinfo articleinfo,HttpServletRequest request) {
        // 1. 非空校验
        if(articleinfo == null || articleinfo.getTitle() == null ||
                articleinfo.getContent() == null || articleinfo.getId() == null) {
            return AjaxResult.fail(-1,"非法参数");
        }
        // 得到当前登录用户
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if (userinfo == null || userinfo.getId() == null) {
            // 无效登录用户
            return AjaxResult.fail(-2,"无效用户");
        }
        // 解决修改文章归属人判定问题
        articleinfo.setUid(userinfo.getId());
        articleinfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleService.update(articleinfo));
    }

    /**
     * 根据分页查询列表
     * @param pindex  当前页码（从1开始）
     * @param psize   每页显示条数
     * @return
     */
    @RequestMapping("/listbypage")
    public AjaxResult getListByPage(Integer pindex,Integer psize) {
        // 1. 参数校正
        if (pindex == null || pindex <= 1) {
            pindex = 1;
        }
        if (psize == null || psize <= 1) {
            psize = 2;
        }
        // 分页公式的值
        int offset = (pindex-1)*psize;
        // 文章列表数据
        List<Articleinfo> list = articleService.getListByPage(psize,offset);
        // 列表总页数
        // 1. 查询一共有多少条数据
        int totalCount = articleService.getCount();
        // 2. 总条数/psize 使用进一法得到总页数
        int pcount = totalCount%psize == 0 ? totalCount/psize : totalCount/psize+1;
        HashMap<String,Object> result = new HashMap<>();
        result.put("list",list);
        result.put("pcount",pcount);
        return AjaxResult.success(result);

    }
}
